package com.yidian.dao;

import com.yidian.pojo.system.Menu;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

/**
 * @author whb
 * @create 2021-06-08 17:27
 * @desciption 菜单Mapper
 */
public interface MenuMapper extends Mapper<Menu> {
    /**
     * 根据登录用户查询菜单列表
     * @param loginName
     * @return
     */
    @Select("SELECT id id,name name,icon icon,url url,parent_id parentId FROM tb_menu WHERE id IN(" +
            "SELECT parent_id FROM tb_menu WHERE id IN(" +
            "SELECT " +
            "DISTINCT f.parent_id " +
            "FROM " +
            "tb_admin a,tb_admin_role b,tb_role c,tb_role_resource d,tb_resource_menu e,tb_menu f " +
            "WHERE " +
            "a.id=b.admin_id " +
            "AND b.role_id=c.id " +
            "AND c.id=d.role_id " +
            "AND d.resource_id=e.resource_id " +
            "AND e.menu_id=f.id " +
            "AND a.login_name=#{loginName} " +
            ") " +
            ") " +
            "UNION " +
            "SELECT id id,name name,icon icon,url url,parent_id parentId FROM tb_menu WHERE id IN(" +
            "SELECT " +
            "DISTINCT f.parent_id " +
            "FROM " +
            "tb_admin a,tb_admin_role b,tb_role c,tb_role_resource d,tb_resource_menu e,tb_menu f " +
            "WHERE " +
            "a.id=b.admin_id " +
            "AND b.role_id=c.id " +
            "AND c.id=d.role_id " +
            "AND d.resource_id=e.resource_id " +
            "AND e.menu_id=f.id " +
            "AND a.login_name=#{loginName} " +
            ") " +
            "UNION " +
            "SELECT " +
            "DISTINCT f.id id,f.name name,f.icon icon,f.url url,f.parent_id parentId " +
            "FROM " +
            "tb_admin a,tb_admin_role b,tb_role c,tb_role_resource d,tb_resource_menu e,tb_menu f " +
            "WHERE " +
            "a.id=b.admin_id " +
            "AND b.role_id=c.id " +
            "AND c.id=d.role_id " +
            "AND d.resource_id=e.resource_id " +
            "AND e.menu_id=f.id " +
            "AND a.login_name=#{loginName}")
    public List<Menu> findMenuByLogin(@Param("loginName") String loginName);
}
